package com.zhihuixingguang.client;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.zhihuixingguang.client.req.LoginReq;

import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

/**
 * 智慧星光上下文
 * 
 * @author 王明伟
 *
 */
@Setter
@Getter
public class ZhihuixingguangContext {

	/**
	 * 用户客户端
	 */
	private static final Map<String, ZhihuixingguangClient> userClient = new ConcurrentHashMap<String, ZhihuixingguangClient>();
	
	/**
	 * 根据登录名获取客户端对象
	 * @param userName
	 * @return
	 */
	public static ZhihuixingguangClient getClient(@NonNull String userName) {
		return userClient.get(userName);
	}
	
	
	public static ZhihuixingguangClient login(@NonNull LoginReq req) {
		ZhihuixingguangClient c = getClient(req.getUserName());
		ZhihuixingguangConfig config = new ZhihuixingguangConfig(req.getUserName(),req.getPassword());
		if(c == null) c = new ZhihuixingguangClientImpl(config);
		userClient.put(req.getUserName(), c);
		return c;
		
	}
}
